package jdbc3;

import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

public class DruidDemo {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/jdbc3/jdbc.propertie");
        properties.load(fileInputStream);
        //使用德鲁伊数据库连接技术
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();
        System.out.println("connection=" + connection);
        String sql="select*from tb_brand";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        ArrayList<Brand> brands = new ArrayList<>();
        while (resultSet.next()){
            Brand brand = new Brand();
            brand.setId(resultSet.getInt("id"));
            brand.setName(resultSet.getString("name"));
            brand.setAge(resultSet.getInt("age"));
            brand.setSex(resultSet.getString("sex"));
            brand.setPhone(resultSet.getInt("phone"));
            brands.add(brand);
        }
        System.out.println("brands = " + brands);
    }
}
